package com.example.cognitivesystemserver.controller;

import com.example.cognitivesystemserver.Exceptions.ParamsException;
import com.example.cognitivesystemserver.base.ResultInfo;
import com.example.cognitivesystemserver.base.ResultType;
import com.example.cognitivesystemserver.pojo.Teacher;
import com.example.cognitivesystemserver.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;


    //添加教师信息
    @PostMapping("/teacher/addTeacher")
    @ResponseBody
    public Object addTeacher(Teacher teacher) {
        ResultInfo resultInfo=new ResultInfo();
        try {
            teacherService.addTeacher(teacher);
            resultInfo.setMsg("添加教师成功！");

        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("添加教师失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    //删除教师信息
    @PostMapping("/teacher/deleteTeacher")
    public Object deleteTeacher(String id) {

        ResultInfo resultInfo=new ResultInfo();
        try {
            teacherService.deleteTeacher(id);
            resultInfo.setMsg("删除教师成功！");
            System.out.println("删除教师成功,id="+id);

        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("删除教师失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    //修改教师信息
    @PostMapping("/teacher/UpdateTeacher")
    public Object updateTeacher(Teacher teacher) {
        ResultInfo resultInfo=new ResultInfo();
        try {
            teacherService.updateTeacher(teacher);
            resultInfo.setMsg("修改教师成功！");

        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("修改教师失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    //查询某个教师信息
    @GetMapping("/teacher/getTeacher")
    public Object getTeacher(String TeacherId) {
        Teacher teacher = teacherService.getTeacher(TeacherId);
        ResultType resultType=new ResultType(0,"",teacher);
        return resultType;
    }

    //查询全部教师信息
    @GetMapping("/teacher/getAllTeacher")
    public Object getAllTeacher() {

        List<Teacher> allTeacher = teacherService.getAllTeacher();
        ResultType resultType=new ResultType(0,"",allTeacher);
        return resultType;
    }
}
